package com.cloud.lawcase.api.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cloud.lawcase.api.bean.LawCauseEditBean;
import com.cloud.lawcase.api.bean.LawCriteriaBean;
import com.cloud.lawcase.api.bean.LawItemCauseBean;
import com.cloud.lawcase.api.bean.PunishDetailsItemBean;
import com.cloud.lawcase.api.constant.LawCaseConstant;
import com.cloud.lawcase.api.entity.TcDicLaw;
import com.cloud.lawcase.api.entity.TcDicLawCause;
import com.cloud.lawcase.api.entity.TcDicLawExcuteType;
import com.cloud.lawcase.api.entity.TcDicLawItem;
import com.cloud.lawcase.api.entity.TcLawCauseSeq;
import com.cloud.lawcase.api.entity.TcLawClDetail;
import com.cloud.lawcase.api.entity.TcLawRelCauseBiz;
import com.cloud.lawcase.api.entity.TcLawRelCauseExcuteType;
import com.cloud.lawcase.api.entity.TcLawRelCauseLawItem;
import com.cloud.lawcase.api.entity.TcRelPunishUnitFunc;
import com.cloud.lawcase.api.entity.TcRelPunishUnitLevel;
import com.cloud.lawcase.api.mapper.TcDicLawCauseMapper;
import com.cloud.lawcase.api.mapper.TcDicLawItemMapper;
import com.cloud.lawcase.api.mapper.TcDicLawMapper;
import com.cloud.lawcase.api.mapper.TcLawCauseSeqMapper;
import com.cloud.lawcase.api.mapper.TcLawClDetailMapper;
import com.cloud.lawcase.api.mapper.TcLawRelCauseBizMapper;
import com.cloud.lawcase.api.mapper.TcLawRelCauseExcuteTypeMapper;
import com.cloud.lawcase.api.mapper.TcLawRelCauseLawItemMapper;
import com.cloud.lawcase.api.mapper.TcRelPunishUnitFuncMapper;
import com.cloud.lawcase.api.mapper.TcRelPunishUnitLevelMapper;
import com.cloud.lawcase.api.service.LawCauseEditService;
import com.cloud.lawcase.api.service.LawItemService;
import com.cloud.lawcase.common.result.QkIcon;
import com.cloud.lawcase.common.result.QkTreeNode;
import com.cloud.lawcase.common.utils.RedisKeyHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;

@Service
@Transactional
public class LawCauseEditServiceImpl implements LawCauseEditService {
	@Autowired
	private TcDicLawMapper tcDicLawMapper;
	@Autowired
	private TcDicLawCauseMapper tcDicLawCauseMapper;
	@Autowired
	private TcLawRelCauseLawItemMapper tcLawRelCauseLawItemMapper;
	@Autowired
	private TcLawRelCauseBizMapper tcLawRelCauseBizMapper;
	@Autowired
	private TcLawRelCauseExcuteTypeMapper tcLawRelCauseExcuteTypeMapper;
	@Autowired
	private LawItemService lawItemService;
	@Autowired
	private TcDicLawItemMapper tcDicLawItemMapper;
	@Autowired
	private TcLawCauseSeqMapper tcLawCauseSeqMapper;
	@Autowired
	private TcRelPunishUnitFuncMapper tcRelPunishUnitFuncMapper;
	@Autowired
	private TcRelPunishUnitLevelMapper tcRelPunishUnitLevelMapper;
	@Autowired
	private TcLawClDetailMapper tcLawClDetailMapper;

	@Override
	public PageInfo<TcDicLaw> listStatPage(LawCriteriaBean criteria) {
		PageInfo<TcDicLaw> page = PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize())
				.doSelectPageInfo(() -> {
					tcDicLawMapper.listPage(criteria);
				});
		List<TcDicLaw> laws = page.getList();
		List<Long> ids = new ArrayList<Long>();
		for (TcDicLaw law : laws) {
			ids.add(law.getLawId());
		}
		if (ids.size() > 0) {
			List<Map<String, Object>> mapRow = tcLawRelCauseExcuteTypeMapper
					.statCauseByLawIds(ids.toArray(new Long[ids.size()]));
			for (TcDicLaw law : laws) {
				for (Map<String, Object> row : mapRow) {
					Long lawId = (Long) row.get("lawId");
					if (law.getLawId().equals(lawId)) {
						law.getStatLists().add(row);
					}
				}
			}
		}
		return page;
	}

	@Override
	public QkTreeNode listTree(long lawId, int excuteTypeId) {
		TcDicLaw law = tcDicLawMapper.selectByPrimaryKey(lawId);
		QkTreeNode rootNode = new QkTreeNode(law.getLawCode(), law.getLawTitle());
		rootNode.setRootFlag(true);
		rootNode.setExpanded(true);
		rootNode.setIcon(QkIcon.FA_BOOK);
		rootNode.setSelectable(false);
		Example example = new Example(TcDicLawCause.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("lawId", lawId);
		if (excuteTypeId > 0) {
			criteria.andEqualTo("excuteTypeId", excuteTypeId);
		}
		List<TcDicLawCause> causes = tcDicLawCauseMapper.selectByExample(example);
		for (TcDicLawCause cause : causes) {
			StringBuilder nodeName = new StringBuilder("【");
			int excuteType = Integer.valueOf(cause.getCauseCode().substring(0, 2));
			if (excuteType > LawCaseConstant.EXCUTE_TYPE_SHORTNAMES.length) {
				nodeName.append(LawCaseConstant.EXCUTE_TYPE_SHORTNAMES[0]);
			} else {
				nodeName.append(LawCaseConstant.EXCUTE_TYPE_SHORTNAMES[excuteType]);
			}
			nodeName.append("】").append(cause.getCauseName());
			QkTreeNode causeNode = new QkTreeNode(cause.getCauseCode(), nodeName.toString());
			causeNode.setSeniorId(law.getLawCode());
			causeNode.setSelectable(true);
			causeNode.setIcon(QkIcon.FA_BOOKMARK);
			causeNode.setLeaf(true);
			causeNode.getData().put("id", cause.getCauseId());
			rootNode.getChildren().add(causeNode);
		}
		return rootNode;
	}

	@Override
	public void deleteLawCause(long causeId) {
		// 案由
		Example lawCauseExample = new Example(TcDicLawCause.class);
		Example.Criteria criteria = lawCauseExample.createCriteria();
		criteria.andEqualTo("causeId", causeId);
		tcDicLawCauseMapper.deleteByExample(lawCauseExample);
		// 条目类别
		Example lawRelCauseItemExample = new Example(TcLawRelCauseLawItem.class);
		Example.Criteria lawRelCauseItemCriteria = lawRelCauseItemExample.createCriteria();
		lawRelCauseItemCriteria.andEqualTo("causeId", causeId);
		tcLawRelCauseLawItemMapper.deleteByExample(lawRelCauseItemExample);
		// 业务分类
		Example lawRelCauseBizExample = new Example(TcLawRelCauseBiz.class);
		Example.Criteria lawRelCauseBizCriteria = lawRelCauseBizExample.createCriteria();
		lawRelCauseBizCriteria.andEqualTo("causeId", causeId);
		tcLawRelCauseBizMapper.deleteByExample(lawRelCauseBizExample);
		// 处罚分类
		Example lawRelCauseExcuteExample = new Example(TcLawRelCauseExcuteType.class);
		Example.Criteria lawRelCauseExcuteCriteria = lawRelCauseExcuteExample.createCriteria();
		lawRelCauseExcuteCriteria.andEqualTo("causeId", causeId);
		tcLawRelCauseExcuteTypeMapper.deleteByExample(lawRelCauseExcuteExample);
		// 事由处罚职能部门
		Example recPunishUnitFuncExample = new Example(TcRelPunishUnitFunc.class);
		Example.Criteria recPunishUnitFuncCriteria = recPunishUnitFuncExample.createCriteria();
		recPunishUnitFuncCriteria.andEqualTo("causeId", causeId);
		tcRelPunishUnitFuncMapper.deleteByExample(recPunishUnitFuncExample);
		// 事由处罚职能层级
		Example recPunishUnitLevelExample = new Example(TcRelPunishUnitLevel.class);
		Example.Criteria recPunishUnitLevelCriteria = recPunishUnitLevelExample.createCriteria();
		recPunishUnitLevelCriteria.andEqualTo("causeId", causeId);
		tcRelPunishUnitLevelMapper.deleteByExample(recPunishUnitLevelExample);
		// 事由处罚罚值
		Example clDetailExample = new Example(TcLawClDetail.class);
		Example.Criteria clDetailCriteria = clDetailExample.createCriteria();
		clDetailCriteria.andEqualTo("causeId", causeId);
		tcLawClDetailMapper.deleteByExample(clDetailExample);
	}

	@Override
	public TcDicLawCause createLawCause(LawCauseEditBean bean) {
		Long causeId = RedisKeyHelper.genereateKey(TcDicLawCause.class);
		if (bean.getExcuteType() == 1) {// 行政处罚
			// 业务分类
			if (!StringUtils.isEmpty(bean.getBizId())) {
				String[] bizs = bean.getBizId().split(",");
				for (String bizId : bizs) {
					TcLawRelCauseBiz biz = new TcLawRelCauseBiz();
					biz.setRelId(RedisKeyHelper.genereateKey(TcLawRelCauseBiz.class));
					biz.setLawId(bean.getLawId());
					biz.setItemId(bean.getItemId());
					biz.setCauseId(causeId);
					biz.setBizId(Integer.valueOf(bizId));
					tcLawRelCauseBizMapper.insert(biz);
				}
			}
			// 条目类别
			bean.setBasisType(2);// 罚则
			// 违则
			Integer[] infringeItemTypes = bean.getInfringeItemTypes();
			long[] infringeItems = bean.getInfringeItems();
			if (null != bean.getInfringeItems() && bean.getInfringeItems().length > 0) {
				for (int i=0,length = infringeItems.length;i<length;i++) {
					TcLawRelCauseLawItem item = new TcLawRelCauseLawItem();
					item.setRelId(RedisKeyHelper.genereateKey(TcLawRelCauseLawItem.class));
					item.setLawId(bean.getLawId());
					item.setItemId(infringeItems[i]);
					item.setCauseId(causeId);
					item.setItemTypeId(infringeItemTypes[i]); // 违则
					tcLawRelCauseLawItemMapper.insert(item);
				}
			}

		} else {
			bean.setBasisType(4); // 直接依据
		}
		// 处罚
		if (null != bean.getPunishs() && bean.getPunishs().size() > 0) {
			for (PunishDetailsItemBean punish : bean.getPunishs()) {
				long relExcuteTypeId = RedisKeyHelper.genereateKey(TcLawRelCauseExcuteType.class);
				// 处罚明细表
				TcLawRelCauseExcuteType relExcuteType = new TcLawRelCauseExcuteType();
				relExcuteType.setPunishId(relExcuteTypeId);
				relExcuteType.setLawId(bean.getLawId());
				relExcuteType.setItemId(bean.getItemId());
				relExcuteType.setCauseId(causeId);
				relExcuteType.setSituationDesc(punish.getSituationDesc());
				relExcuteType.setExcuteTypeId(punish.getPunishType().getExcuteTypeId());
				tcLawRelCauseExcuteTypeMapper.insert(relExcuteType);
				// 处罚职能部门表
				List<TcRelPunishUnitFunc> unitFuncs = punish.getFuncUnits();
				if (null != unitFuncs && unitFuncs.size() > 0) {
					for (TcRelPunishUnitFunc func : unitFuncs) {
						func.setRelId(RedisKeyHelper.genereateKey(TcRelPunishUnitFunc.class));
						func.setCauseId(causeId);
						func.setLawId(bean.getLawId());
						func.setPunishId(relExcuteTypeId);
						tcRelPunishUnitFuncMapper.insert(func);
					}
				}
				// 处罚职能级别表
				List<TcRelPunishUnitLevel> unitLevels = punish.getFuncLevels();
				if (null != unitLevels && unitLevels.size() > 0) {
					for (TcRelPunishUnitLevel level : unitLevels) {
						level.setRelId(RedisKeyHelper.genereateKey(TcRelPunishUnitLevel.class));
						level.setCauseId(causeId);
						level.setLawId(bean.getLawId());
						level.setPunishId(relExcuteTypeId);
						tcRelPunishUnitLevelMapper.insert(level);
					}
				}
				// 处罚裁量明细
				List<TcLawClDetail> clDetails = punish.getClDetails();
				if (null != clDetails && clDetails.size() > 0) {
					for (TcLawClDetail clDetail : punish.getClDetails()) {
						clDetail.setClDetailId(RedisKeyHelper.genereateKey(TcLawClDetail.class));
						clDetail.setLawId(bean.getLawId());
						clDetail.setCauseId(causeId);
						clDetail.setPunishId(relExcuteTypeId);
						tcLawClDetailMapper.insert(clDetail);
					}
				}
			}
		}

		// 条目类别：罚则依据
		TcLawRelCauseLawItem item = new TcLawRelCauseLawItem();
		item.setRelId(RedisKeyHelper.genereateKey(TcLawRelCauseLawItem.class));
		item.setLawId(bean.getLawId());
		item.setItemId(bean.getItemId());
		item.setCauseId(causeId);
		item.setItemTypeId(bean.getBasisType());
		tcLawRelCauseLawItemMapper.insert(item);
		// 案由
		TcDicLawCause lawCause = new TcDicLawCause();
		// 事项规编码
		TcDicLaw law = tcDicLawMapper.selectByPrimaryKey(bean.getLawId());
		if (!StringUtils.isEmpty(law.getLawCode())) {
			lawCause.setCauseCode(this.calculateLawCauseCode(law, bean.getExcuteType()));
		}
		lawCause.setCauseId(causeId);
		lawCause.setLawId(bean.getLawId());
		lawCause.setItemId(bean.getItemId());
		lawCause.setCauseName(bean.getCauseName());
		lawCause.setIsPunish(bean.getPunish());
		lawCause.setExcuteTypeId(bean.getExcuteType());
		tcDicLawCauseMapper.insert(lawCause);
		return lawCause;
	}

	@Override
	public TcDicLawCause updateLawCause(LawCauseEditBean bean) {
		// 案由信息
		TcDicLawCause cause = tcDicLawCauseMapper.selectByPrimaryKey(bean.getCauseId());
		cause.setIsPunish(bean.getPunish());
		cause.setCauseName(bean.getCauseName());
		tcDicLawCauseMapper.updateByPrimaryKey(cause);
		// 行政处罚
		if (bean.getExcuteType() == 1) {
			// 业务
			tcLawRelCauseBizMapper.deleteByCauseId(bean.getCauseId());
			if (!StringUtils.isEmpty(bean.getBizId())) {
				String[] bizs = bean.getBizId().split(",");
				for (String bizId : bizs) {
					TcLawRelCauseBiz biz = new TcLawRelCauseBiz();
					biz.setRelId(RedisKeyHelper.genereateKey(TcLawRelCauseBiz.class));
					biz.setLawId(cause.getLawId());
					biz.setItemId(cause.getItemId());
					biz.setCauseId(cause.getCauseId());
					biz.setBizId(Integer.valueOf(bizId));
					tcLawRelCauseBizMapper.insert(biz);
				}
			}
			// 违则
			tcLawRelCauseLawItemMapper.deleteByCauseId(bean.getCauseId());
			if (null != bean.getInfringeItems() && bean.getInfringeItems().length > 0) {
				Long itemId = null;
				long[] infringeItems = bean.getInfringeItems();
				Integer[] infringeItemTypes = bean.getInfringeItemTypes();
				int length = infringeItems.length;
				for (int i = 0; i < length; i++) {
					itemId = infringeItems[i];
					TcLawRelCauseLawItem item = new TcLawRelCauseLawItem();
					item.setRelId(RedisKeyHelper.genereateKey(TcLawRelCauseLawItem.class));
					item.setLawId(cause.getLawId());
					item.setItemId(itemId);
					item.setCauseId(cause.getCauseId());
					// 违则或准用依据
					item.setItemTypeId(infringeItemTypes[i]);
					tcLawRelCauseLawItemMapper.insert(item);
				}
			}
		}
		// 处罚
		tcLawRelCauseExcuteTypeMapper.deleteByCauseId(bean.getCauseId());
		tcRelPunishUnitFuncMapper.deleteByCauseId(bean.getCauseId());
		tcRelPunishUnitLevelMapper.deleteByCauseId(bean.getCauseId());
		tcLawClDetailMapper.deleteByCauseId(bean.getCauseId());
		if (null != bean.getPunishs() && bean.getPunishs().size() > 0) {
			for (PunishDetailsItemBean punish : bean.getPunishs()) {
				long relExcuteTypeId = RedisKeyHelper.genereateKey(TcLawRelCauseExcuteType.class);
				// 处罚明细表
				TcLawRelCauseExcuteType relExcuteType = new TcLawRelCauseExcuteType();
				relExcuteType.setPunishId(relExcuteTypeId);
				relExcuteType.setLawId(bean.getLawId());
				relExcuteType.setItemId(bean.getItemId());
				relExcuteType.setCauseId(bean.getCauseId());
				relExcuteType.setSituationDesc(punish.getSituationDesc());
				relExcuteType.setExcuteTypeId(punish.getPunishType().getExcuteTypeId());
				tcLawRelCauseExcuteTypeMapper.insert(relExcuteType);
				// 处罚职能部门表
				List<TcRelPunishUnitFunc> unitFuncs = punish.getFuncUnits();
				if (null != unitFuncs && unitFuncs.size() > 0) {
					for (TcRelPunishUnitFunc func : unitFuncs) {
						func.setRelId(RedisKeyHelper.genereateKey(TcRelPunishUnitFunc.class));
						func.setCauseId(bean.getCauseId());
						func.setLawId(bean.getLawId());
						func.setPunishId(relExcuteTypeId);
						tcRelPunishUnitFuncMapper.insert(func);
					}
				}
				// 处罚职能级别表
				List<TcRelPunishUnitLevel> unitLevels = punish.getFuncLevels();
				if (null != unitLevels && unitLevels.size() > 0) {
					for (TcRelPunishUnitLevel level : unitLevels) {
						level.setRelId(RedisKeyHelper.genereateKey(TcRelPunishUnitLevel.class));
						level.setCauseId(bean.getCauseId());
						level.setLawId(bean.getLawId());
						level.setPunishId(relExcuteTypeId);
						tcRelPunishUnitLevelMapper.insert(level);
					}
				}
				// 处罚裁量明细
				List<TcLawClDetail> clDetails = punish.getClDetails();
				if (null != clDetails && clDetails.size() > 0) {
					for (TcLawClDetail clDetail : punish.getClDetails()) {
						clDetail.setClDetailId(RedisKeyHelper.genereateKey(TcLawClDetail.class));
						clDetail.setLawId(bean.getLawId());
						clDetail.setCauseId(bean.getCauseId());
						clDetail.setPunishId(relExcuteTypeId);
						tcLawClDetailMapper.insert(clDetail);
					}
				}
			}
		}
		return cause;
	}

	@Override
	public List<TcDicLawItem> listItemCause(long lawId, int excuteTypeId) {
		List<TcDicLawItem> items = lawItemService.loadLawItems(lawId);
		// 案由事项
		List<TcDicLawCause> itemCauses = tcDicLawCauseMapper.listCauseByLawAndExcute(lawId, excuteTypeId);
		// 业务类型
		Example lawRelCauseBizExample = new Example(TcLawRelCauseBiz.class);
		Example.Criteria lawRelCauseBizCriteria = lawRelCauseBizExample.createCriteria();
		lawRelCauseBizCriteria.andEqualTo("lawId", lawId);
		List<TcLawRelCauseBiz> causeBizs = tcLawRelCauseBizMapper.selectByExample(lawRelCauseBizExample);
		// 条目类别
		List<TcLawRelCauseLawItem> itemTypes = tcLawRelCauseLawItemMapper.listRelCauseItemByLawId(lawId);
		// 处罚种类、职能部门、职能级别、裁量明细
		List<TcLawRelCauseExcuteType> punishTypes = tcLawRelCauseExcuteTypeMapper.listExceuteTypeByLawId(lawId);
		List<TcRelPunishUnitFunc> unitFuncs = tcRelPunishUnitFuncMapper.listUnitFuncByLawId(lawId);
		List<TcRelPunishUnitLevel> unitLevels = tcRelPunishUnitLevelMapper.listUnitLevelByLawId(lawId);
		Example example = new Example(TcLawClDetail.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("lawId", lawId);
		List<TcLawClDetail> clDetails = tcLawClDetailMapper.selectByExample(example);

		List<LawItemCauseBean> causes = new ArrayList<LawItemCauseBean>();
		for (TcDicLawCause item : itemCauses) {
			LawItemCauseBean bean = new LawItemCauseBean();
			bean.setLawId(item.getLawId());
			bean.setItemId(item.getItemId());
			bean.setCauseId(item.getCauseId());
			bean.setCauseCode(item.getCauseCode());
			bean.setCauseName(item.getCauseName());
			bean.setIsPunish(item.getIsPunish());
			bean.setExcuteTypeId(item.getExcuteTypeId());
			bean.setExcuteTypeName(item.getExcuteTypeName());

			// 行政处罚
			if (bean.getExcuteTypeId() == 1) {
				// 业务类别
				for (TcLawRelCauseBiz biz : causeBizs) {
					if (item.getCauseId().equals(biz.getCauseId())) {
						if (StringUtils.isEmpty(bean.getBizId())) {
							bean.setBizId(biz.getBizId() + ",");
						} else {
							bean.setBizId(bean.getBizId() + biz.getBizId() + ",");
						}
					}
				}
				// 条目类别
				List<Long> ids = new ArrayList<Long>();
				for (TcLawRelCauseLawItem itemType : itemTypes) {
					if (item.getCauseId().equals(itemType.getCauseId())
							&& (itemType.getItemTypeId() == 1 || itemType.getItemTypeId() == 10)) {// 违则
						ids.add(itemType.getItemId());
					}
				}
				if (ids.size() > 0) {
					// 违则和准用依据
					bean.setItems(tcDicLawItemMapper.listInfringeItems(ids, item.getCauseId()));
				}

			}
			// 条目类别
			StringBuilder itemTypeName=new StringBuilder();
			for (TcLawRelCauseLawItem itemType : itemTypes) {
				if (item.getCauseId().equals(itemType.getCauseId()) && itemType.getItemTypeId() != 1) {// 非违则
					bean.setItemTypeId(itemType.getItemTypeId());
					itemTypeName.append(itemType.getItemTypeName());
					itemTypeName.append(" ");
				}
			}
			bean.setItemTypeName(itemTypeName.toString());



			// 处罚明细
			StringBuilder punishTypeStr = new StringBuilder();
			for (TcLawRelCauseExcuteType punish : punishTypes) {
				if (punish.getCauseId().equals(item.getCauseId())) {
					// 处罚
					PunishDetailsItemBean punishDetailItem = new PunishDetailsItemBean();
					TcDicLawExcuteType excuteType = new TcDicLawExcuteType();
					excuteType.setExcuteTypeId(punish.getExcuteTypeId());
					excuteType.setExcuteTypeName(punish.getExcuteTypeName());
					punishDetailItem.setPunishType(excuteType);
					punishDetailItem.setSituationDesc(punish.getSituationDesc());

					punishTypeStr.append(punish.getExcuteTypeName()).append(",");
					// 职能部门
					for (TcRelPunishUnitFunc func : unitFuncs) {
						if (func.getCauseId().equals(item.getCauseId())
								&& punish.getPunishId().equals(func.getPunishId())) {
							punishDetailItem.getFuncUnits().add(func);
						}
					}
					// 职能层级
					for (TcRelPunishUnitLevel level : unitLevels) {
						if (level.getCauseId().equals(item.getCauseId())
								&& punish.getPunishId().equals(level.getPunishId())) {
							punishDetailItem.getFuncLevels().add(level);
						}
					}
					// 裁量明细
					for (TcLawClDetail clDetail : clDetails) {
						if (clDetail.getCauseId().equals(item.getCauseId())
								&& punish.getPunishId().equals(clDetail.getPunishId())) {
							if (clDetail.getTopUnit() == 1) {
								clDetail.setTopUnitName("元");
							} else if (clDetail.getTopUnit() == 2) {
								clDetail.setTopUnitName("倍");
							}
							if (clDetail.getLowerUnit() == 1) {
								clDetail.setLowerUnitName("元");
							} else if (clDetail.getLowerUnit() == 2) {
								clDetail.setLowerUnitName("倍");
							}
							punishDetailItem.getClDetails().add(clDetail);
						}
					}
					bean.getPunishs().add(punishDetailItem);
				}
			}
			bean.setPunishName(punishTypeStr.toString());
			causes.add(bean);
		}

		// 封装条目与事由
		for (TcDicLawItem item : items) {
			for (LawItemCauseBean cause : causes) {
				if (item.getItemId().equals(cause.getItemId())) {
					item.getChildren().add(cause);
				}
			}
		}
		return items;
	}

	@Override
	public int copyLawCause(long srcId, long targetId) {
		TcDicLaw targetLaw = tcDicLawMapper.selectByPrimaryKey(targetId);
		// 事由表
		// 用于缓存新老案由的对应关系
		Map<Long, Long> causeIdCache = new HashMap<Long, Long>();
		Example srcCauseExample = new Example(TcDicLawCause.class);
		Example.Criteria srcCauseCriteria = srcCauseExample.createCriteria();
		srcCauseCriteria.andEqualTo("lawId", srcId);
		List<TcDicLawCause> srcCauses = tcDicLawCauseMapper.selectByExample(srcCauseExample);
		for (TcDicLawCause cause : srcCauses) {
			// 找到原法规对应的条目
			TcDicLawItem srcItem = tcDicLawItemMapper.selectByPrimaryKey(cause.getItemId());
			srcItem.setLawId(targetId);
			List<Long> targetItems = tcDicLawItemMapper.queryItemIdByLawAndChapter(srcItem);
			if (null != targetItems && targetItems.size() == 1) {
				long targetCauseId = RedisKeyHelper.genereateKey(TcDicLawCause.class);
				TcDicLawCause targetCause = new TcDicLawCause();
				BeanUtils.copyProperties(cause, targetCause);
				targetCause.setLawId(targetId);
				targetCause.setItemId(targetItems.get(0));
				targetCause.setCauseId(targetCauseId);
				targetCause.setCauseCode(this.calculateLawCauseCode(targetLaw, targetCause.getExcuteTypeId()));
				tcDicLawCauseMapper.insert(targetCause);
				causeIdCache.put(cause.getCauseId(), targetCauseId);
			}
		}
		// 事由条目关联表
		Example relCauseLawItemsExample = new Example(TcLawRelCauseLawItem.class);
		Example.Criteria relCauseLawItemsCriteria = relCauseLawItemsExample.createCriteria();
		relCauseLawItemsCriteria.andEqualTo("lawId", srcId);
		List<TcLawRelCauseLawItem> items = tcLawRelCauseLawItemMapper.selectByExample(relCauseLawItemsExample);
		for (TcLawRelCauseLawItem item : items) {
			TcDicLawItem srcItem = tcDicLawItemMapper.selectByPrimaryKey(item.getItemId());
			// 找到原始法规对应的条目
			srcItem.setLawId(targetId);
			List<Long> targetItems = tcDicLawItemMapper.queryItemIdByLawAndChapter(srcItem);
			if (null != targetItems && targetItems.size() == 1) {
				if (null != causeIdCache.get(item.getCauseId())) {
					long targetRelItemId = RedisKeyHelper.genereateKey(TcLawRelCauseLawItem.class);
					TcLawRelCauseLawItem relCauseLawItem = new TcLawRelCauseLawItem();
					BeanUtils.copyProperties(item, relCauseLawItem);
					relCauseLawItem.setRelId(targetRelItemId);
					relCauseLawItem.setLawId(targetId);
					relCauseLawItem.setCauseId(causeIdCache.get(item.getCauseId()));
					relCauseLawItem.setItemId(targetItems.get(0));
					tcLawRelCauseLawItemMapper.insert(relCauseLawItem);
				}
			}
		}
		// 事由业务关联表
		Example relBizCauseExample = new Example(TcLawRelCauseBiz.class);
		Example.Criteria relBizCauseCriteria = relBizCauseExample.createCriteria();
		relBizCauseCriteria.andEqualTo("lawId", srcId);
		List<TcLawRelCauseBiz> relBizes = tcLawRelCauseBizMapper.selectByExample(relBizCauseExample);
		for (TcLawRelCauseBiz relBiz : relBizes) {
			TcDicLawItem srcItem = tcDicLawItemMapper.selectByPrimaryKey(relBiz.getItemId());
			srcItem.setLawId(targetId);
			List<Long> targetItems = tcDicLawItemMapper.queryItemIdByLawAndChapter(srcItem);
			if (null != targetItems && targetItems.size() == 1) {
				if (null != causeIdCache.get(relBiz.getCauseId())) {
					long targetRelBizId = RedisKeyHelper.genereateKey(TcLawRelCauseBiz.class);
					TcLawRelCauseBiz targetBiz = new TcLawRelCauseBiz();
					targetBiz.setBizId(relBiz.getBizId());
					targetBiz.setRelId(targetRelBizId);
					targetBiz.setLawId(targetId);
					targetBiz.setItemId(targetItems.get(0));
					targetBiz.setCauseId(causeIdCache.get(relBiz.getCauseId()));
				}
			}
		}
		// 事由处罚表
		Example lawRelCauseExcuteExample = new Example(TcLawRelCauseExcuteType.class);
		Example.Criteria lawRelCauseExcuteCriteria = lawRelCauseExcuteExample.createCriteria();
		lawRelCauseExcuteCriteria.andEqualTo("lawId", srcId);
		List<TcLawRelCauseExcuteType> types = tcLawRelCauseExcuteTypeMapper.selectByExample(lawRelCauseExcuteExample);
		for(TcLawRelCauseExcuteType type : types){
			TcDicLawItem srcItem = tcDicLawItemMapper.selectByPrimaryKey(type.getItemId());
			srcItem.setLawId(targetId);
			List<Long> targetItems = tcDicLawItemMapper.queryItemIdByLawAndChapter(srcItem);
			if (null != targetItems && targetItems.size() == 1) {
				if (null != causeIdCache.get(type.getCauseId())) {
					long targetPunishId = RedisKeyHelper.genereateKey(TcLawRelCauseExcuteType.class);
					TcLawRelCauseExcuteType targetRelCuseExcuteType = new TcLawRelCauseExcuteType();
					targetRelCuseExcuteType.setSituationDesc(type.getSituationDesc());
					targetRelCuseExcuteType.setExcuteTypeId(type.getExcuteTypeId());
					targetRelCuseExcuteType.setPunishId(targetPunishId);
					targetRelCuseExcuteType.setLawId(targetId);
					targetRelCuseExcuteType.setItemId(targetItems.get(0));
					targetRelCuseExcuteType.setCauseId(causeIdCache.get(type.getCauseId()));
					tcLawRelCauseExcuteTypeMapper.insert(targetRelCuseExcuteType);
					
					// 事由处罚裁量明细表
					Example clDetailExample = new Example(TcLawClDetail.class);
					Example.Criteria clDetailCriteria = clDetailExample.createCriteria();
					clDetailCriteria.andEqualTo("punishId", type.getPunishId());
					List<TcLawClDetail> clDetails = tcLawClDetailMapper.selectByExample(clDetailExample);
					for(TcLawClDetail clDetail : clDetails){
						clDetail.setClDetailId(RedisKeyHelper.genereateKey(TcLawClDetail.class));
						clDetail.setLawId(targetId);
						clDetail.setItemId(targetItems.get(0));
						clDetail.setCauseId(causeIdCache.get(type.getCauseId()));
						clDetail.setPunishId(targetPunishId);
						tcLawClDetailMapper.insert(clDetail);
					}
					// 事由处罚职能部门关联表
					Example clUnitTypeExample = new Example(TcRelPunishUnitFunc.class);
					Example.Criteria clTypeCriteria = clUnitTypeExample.createCriteria();
					clTypeCriteria.andEqualTo("punishId", type.getPunishId());
					List<TcRelPunishUnitFunc> funcs = tcRelPunishUnitFuncMapper.selectByExample(clUnitTypeExample);
					for(TcRelPunishUnitFunc func : funcs){
						func.setRelId(RedisKeyHelper.genereateKey(TcRelPunishUnitFunc.class));
						func.setPunishId(targetPunishId);
						func.setLawId(targetId);
						func.setCauseId(causeIdCache.get(type.getCauseId()));
						tcRelPunishUnitFuncMapper.insert(func);
					}
					// 事由处罚职能级别关联表
					Example clUnitLevelExample = new Example(TcRelPunishUnitLevel.class);
					Example.Criteria clLevelCriteria = clUnitLevelExample.createCriteria();
					clLevelCriteria.andEqualTo("punishId", type.getPunishId());
					List<TcRelPunishUnitLevel> levels = tcRelPunishUnitLevelMapper.selectByExample(clUnitTypeExample);
					for(TcRelPunishUnitLevel level : levels){
						level.setRelId(RedisKeyHelper.genereateKey(TcRelPunishUnitFunc.class));
						level.setPunishId(targetPunishId);
						level.setLawId(targetId);
						level.setCauseId(causeIdCache.get(type.getCauseId()));
						tcRelPunishUnitLevelMapper.insert(level);
					}
				}
			}
		}
		return srcCauses.size();
	}

	/**
	 * 计算案由编码
	 * 
	 * @param law
	 * @param excuteType
	 * @return
	 */
	private String calculateLawCauseCode(TcDicLaw law, Integer excuteType) {
		StringBuilder lawCode = new StringBuilder(String.format("%02d", excuteType));
		lawCode.append(law.getLawCode().substring(2, law.getLawCode().length()));
		// 查找事由执法类别对应的序列号
		TcLawCauseSeq pk = new TcLawCauseSeq();
		pk.setLawId(law.getLawId());
		pk.setExcuteTypeId(excuteType);
		TcLawCauseSeq seq = tcLawCauseSeqMapper.selectByPrimaryKey(pk);
		if (null == seq) {
			seq = new TcLawCauseSeq();
			seq.setLawId(law.getLawId());
			seq.setExcuteTypeId(excuteType);
			seq.setSeqValue(1);
			lawCode.append(String.format("%03d", 1));
			tcLawCauseSeqMapper.insert(seq);
		} else {
			int value = seq.getSeqValue();
			lawCode.append(String.format("%03d", value + 1));
			seq.setSeqValue(value + 1);
			tcLawCauseSeqMapper.updateByPrimaryKey(seq);
		}
		return lawCode.toString();

	}
}
